package com.globallogic.tokentreebuilder;

import com.globallogic.addtoken.TokenHandlerCircle;
import com.globallogic.addtoken.TokenHandlerRectangle;
import com.globallogic.addtoken.TokenHandlerSquare;
import com.globallogic.drawtoken.TokenHandlerDrawCircle;
import com.globallogic.drawtoken.TokenHandlerDrawRectangle;
import com.globallogic.drawtoken.TokenHandlerDrawSquare;
import com.globallogic.token.TokenHandlerGetSquare;
import com.globallogic.token.TokenHandlerHelp;
import com.globallogic.token.TokenHandlerList;
import com.globallogic.token.TokenHandlerPtInside;
import com.globallogic.token.TokenProcessor;
import com.globallogic.token.TokenType;

public class TokenTreeBuilder {
	
	public static TokenProcessor getTokenTree() {
		
		StringBuilder stringBuilder = new StringBuilder();
		
		TokenProcessor root = new TokenProcessor("", TokenType.COMMAND, null, null);
		TokenProcessor tokenProcessor;
		
		root.addChildTokenProcessor(buildAddToken(root));
		root.addChildTokenProcessor(buildDrawToken(root));

		stringBuilder.append(" - is used for getting information about square of figures");
//		stringBuilder.append(lineSeparator);		
		tokenProcessor = new TokenProcessor("getsq", TokenType.COMMAND, new TokenHandlerGetSquare(), root);
		tokenProcessor.setDescription(stringBuilder.toString());
		root.addChildTokenProcessor(tokenProcessor);

		stringBuilder.setLength(0);		
		stringBuilder.append(" <x> <y> - is used to check if point is inside figures.");
		stringBuilder.append(getLineSeparator());
		stringBuilder.append("         <x> and <y> should be an integer values");
//		stringBuilder.append(lineSeparator);
		
		tokenProcessor = new TokenProcessor("ptinside", TokenType.COMMAND, new TokenHandlerPtInside(), root);
		tokenProcessor.setDescription(stringBuilder.toString());		
		root.addChildTokenProcessor(tokenProcessor);

		stringBuilder.setLength(0);
		stringBuilder.append(" - is used to show all existings figures");
//		stringBuilder.append(lineSeparator);

		tokenProcessor = new TokenProcessor("list", TokenType.COMMAND, new TokenHandlerList(), root);
		tokenProcessor.setDescription(stringBuilder.toString());
		root.addChildTokenProcessor(tokenProcessor);
		
		stringBuilder.setLength(0);
		stringBuilder.append(" - is used to show help");
//		stringBuilder.append(lineSeparator);

		tokenProcessor = new TokenProcessor("help", TokenType.COMMAND, new TokenHandlerHelp(), root);
		tokenProcessor.setDescription(stringBuilder.toString());
		root.addChildTokenProcessor(tokenProcessor);
		
		return root;
		
	}
	
    private static TokenProcessor buildAddToken(TokenProcessor root) {

    	TokenProcessor parentTokenProcessor = null;
    	TokenProcessor childTokenProcessor = null;

    	StringBuilder stringBuilder = new StringBuilder();
    	
    	parentTokenProcessor = new TokenProcessor("add", TokenType.COMMAND,  null, root);
    	stringBuilder.append(" <figure> <params> - is used for adding new figure. List of available figures:");
    	parentTokenProcessor.setDescription(stringBuilder.toString());
    	
    	stringBuilder.setLength(0);
    	stringBuilder.append(" <radius> - is used to add new circle. <radius> is a radius of new circle. Should be an integer value");
    	childTokenProcessor = new TokenProcessor("circle", TokenType.FIGURE, new TokenHandlerCircle(), parentTokenProcessor);
    	childTokenProcessor.setDescription(stringBuilder.toString());
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);
    	
    	stringBuilder.setLength(0);
    	stringBuilder.append(" <side> - is used to add new square. <side> is a side of new square. Should be an integer value");
    	childTokenProcessor = new TokenProcessor("square", TokenType.FIGURE, new TokenHandlerSquare(), parentTokenProcessor);
    	childTokenProcessor.setDescription(stringBuilder.toString());
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);

    	stringBuilder.setLength(0);
    	childTokenProcessor = new TokenProcessor("rectangle", TokenType.FIGURE, new TokenHandlerRectangle(), parentTokenProcessor);
    	stringBuilder.append(" <width> <height> - is used to add new rectangle. <width> and <height> is a side of new square.");
    	stringBuilder.append(getLineSeparator());
    	stringBuilder.append("              <width> and <height> should be an integer value");
    	
    	childTokenProcessor.setDescription(stringBuilder.toString());
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);
    	
		return parentTokenProcessor;
    	
    }
	
    private static TokenProcessor buildDrawToken(TokenProcessor root) {

    	TokenProcessor parentTokenProcessor = null;
    	TokenProcessor childTokenProcessor = null;
    	
    	parentTokenProcessor = new TokenProcessor("draw", TokenType.COMMAND,  null, root);
    	parentTokenProcessor.setDescription("<figure> - is user to draw selected type of figures. List of availible options is:");
    	
    	childTokenProcessor = new TokenProcessor("circle", TokenType.FIGURE, new TokenHandlerDrawCircle(), parentTokenProcessor);
    	childTokenProcessor.setDescription(" - is used to draw circles");
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);

    	childTokenProcessor = new TokenProcessor("square", TokenType.FIGURE, new TokenHandlerDrawSquare(), parentTokenProcessor);
    	childTokenProcessor.setDescription(" - is used to draw squares");
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);
    	
    	childTokenProcessor = new TokenProcessor("rectangle", TokenType.FIGURE, new TokenHandlerDrawRectangle(), parentTokenProcessor);
    	childTokenProcessor.setDescription(" - is used to draw rectangles");
    	parentTokenProcessor.addChildTokenProcessor(childTokenProcessor);
    	
		return parentTokenProcessor;
    	
    }
    
	public static String getLineSeparator() {
		return lineSeparator;
	}

	private static String lineSeparator = System.getProperty("line.separator");

    
}
